package com.eshop.modules.mp.config;

import com.google.common.collect.Maps;
import org.slf4j.LoggerFactory;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.eshop.utils.RedisUtil;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.eshop.utils.ShopKeyUtils;
import com.eshop.enums.PayMethodEnum;
import org.springframework.beans.factory.annotation.Autowired;
import com.eshop.utils.RedisUtils;
import com.github.binarywang.wxpay.service.WxPayService;
import java.util.Map;
import org.slf4j.Logger;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class WxPayConfiguration
{
    private static final Logger log;
    private static Map<String, WxPayService> payServices;
    private static RedisUtils redisUtils;
    
    @Autowired
    public WxPayConfiguration(final RedisUtils redisUtils) {
        WxPayConfiguration.redisUtils = redisUtils;
    }
    
    public static WxPayService getPayService(final PayMethodEnum payMethodEnum) {
        WxPayService wxPayService = WxPayConfiguration.payServices.get(ShopKeyUtils.getYshopWeiXinPayService() + payMethodEnum.getValue());
        if (wxPayService == null || WxPayConfiguration.redisUtils.get(ShopKeyUtils.getYshopWeiXinPayService()) == null) {
            final WxPayConfig payConfig = new WxPayConfig();
            switch (payMethodEnum) {
                case WECHAT: {
                    payConfig.setAppId(WxPayConfiguration.redisUtils.getY(ShopKeyUtils.getWechatAppId()));
                    break;
                }
                case WXAPP: {
                    payConfig.setAppId((String)RedisUtil.get(ShopKeyUtils.getWxAppAppId()));
                    break;
                }
                case APP: {
                    payConfig.setAppId((String)RedisUtil.get(ShopKeyUtils.getWxNativeAppAppId()));
                    break;
                }
            }
            payConfig.setMchId(WxPayConfiguration.redisUtils.getY(ShopKeyUtils.getWxPayMchId()));
            payConfig.setMchKey(WxPayConfiguration.redisUtils.getY(ShopKeyUtils.getWxPayMchKey()));
            payConfig.setKeyPath(WxPayConfiguration.redisUtils.getY(ShopKeyUtils.getWxPayKeyPath()));
            payConfig.setUseSandboxEnv(false);
            wxPayService = (WxPayService)new WxPayServiceImpl();
            wxPayService.setConfig(payConfig);
            WxPayConfiguration.payServices.put(ShopKeyUtils.getYshopWeiXinPayService() + payMethodEnum.getValue(), wxPayService);
            WxPayConfiguration.redisUtils.set(ShopKeyUtils.getYshopWeiXinPayService(), (Object)"yshop");
        }
        return wxPayService;
    }
    
    public static void removeWxPayService() {
        WxPayConfiguration.redisUtils.del(new String[] { ShopKeyUtils.getYshopWeiXinPayService() });
        WxPayConfiguration.payServices.remove(ShopKeyUtils.getYshopWeiXinPayService());
    }
    
    static {
        log = LoggerFactory.getLogger(WxPayConfiguration.class);
        WxPayConfiguration.payServices = Maps.newHashMap();
    }
}
